//
//  UIView+KDJFrame.m
//  BuDeJie
//
//  Created by Kavee DJ on 2016/11/1.
//  Copyright © 2016年 Kavee DJ. All rights reserved.
//

#import "UIView+KDJFrame.h"

@implementation UIView (KDJFrame)

- (BOOL)kdj_intersectWithView:(UIView *)view
{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    CGRect selfRect = [self convertRect:self.bounds toView:window];
    CGRect viewRect = [view convertRect:view.bounds toView:window];
    return CGRectIntersectsRect(selfRect, viewRect);
}

- (UIViewController *)kdj_parentController
{
    UIResponder *responder = [self nextResponder];
    while (responder) {
        if ([responder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)responder;
        }
        responder = [responder nextResponder];
    }
    return nil;
}

+ (UIViewController *)kdj_currentViewController
{
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    // modal展现方式的底层视图不同
    // 取到第一层时，取到的是UITransitionView，通过这个view拿不到控制器
    UIView *firstView = [keyWindow.subviews firstObject];
    UIView *secondView = [firstView.subviews firstObject];
    UIViewController *vc = secondView.kdj_parentController;
    if ([vc isKindOfClass:[UITabBarController class]]) {
        UITabBarController *tab = (UITabBarController *)vc;
        if ([tab.selectedViewController isKindOfClass:[UINavigationController class]]) {
            UINavigationController *nav = (UINavigationController *)tab.selectedViewController;
            return [nav.viewControllers lastObject];
        } else {
            return tab.selectedViewController;
        }
    } else if ([vc isKindOfClass:[UINavigationController class]]) {
        UINavigationController *nav = (UINavigationController *)vc;
        return [nav.viewControllers lastObject];
    } else {
        return vc;
    }
    return nil;
}

+ (instancetype)kdj_viewFromXib
{
    return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil].firstObject;
}

- (void)setKdj_height:(CGFloat)kdj_height
{
    CGRect rect = self.frame;
    rect.size.height = kdj_height;
    self.frame = rect;
}

- (CGFloat)kdj_height
{
    return self.frame.size.height;
}

- (CGFloat)kdj_width
{
    return self.frame.size.width;
}
- (void)setKdj_width:(CGFloat)kdj_width
{
    CGRect rect = self.frame;
    rect.size.width = kdj_width;
    self.frame = rect;
}

- (CGFloat)kdj_x
{
    return self.frame.origin.x;
    
}

- (void)setKdj_x:(CGFloat)kdj_x
{
    CGRect rect = self.frame;
    rect.origin.x = kdj_x;
    self.frame = rect;
}

- (void)setKdj_y:(CGFloat)kdj_y
{
    CGRect rect = self.frame;
    rect.origin.y = kdj_y;
    self.frame = rect;
}

- (CGFloat)kdj_y
{
    
    return self.frame.origin.y;
}

- (void)setKdj_centerX:(CGFloat)kdj_centerX
{
    CGPoint center = self.center;
    center.x = kdj_centerX;
    self.center = center;
}

- (CGFloat)kdj_centerX
{
    return self.center.x;
}

- (void)setKdj_centerY:(CGFloat)kdj_centerY
{
    CGPoint center = self.center;
    center.y = kdj_centerY;
    self.center = center;
}

- (CGFloat)kdj_centerY
{
    return self.center.y;
}

@end
